package cfca.paperless.client.servlet._3_assist._3_12_AutoUseSealImage;

import cfca.paperless.client.bean.SealImageUserInfo;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.StringUtil;
import cfca.sadk.org.bouncycastle.util.encoders.Base64;

/**
 * @Description 3.9 自助生成图片接口（测试用例）
 * @Author dell
 * @Date 2016-5-16
 * @CodeReviewer TODO
 */
public class AutoUseSealImageTest01 {

    public static void main(String[] args) throws Exception {
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;

            String sealImageCode = "ym201712757438642";
            String bizSerialNo = GUID.generateId();
            String businessCode = "000006789baG";
            String businessCodeStyle = "font-family:宋体;font-size:12;x-ratio:0.1;y-ratio:0.5;codeColor:#0000ff";
//            String businessCodeStyle = "";
            String sealPerson = "张三";
            String sealLocation = "bj";
            String sealReason = "电子合同";
            String department = "办公室";
            String remark1 = "remark1";
            String remark2 = "remark2";
            String remark3 = "remark3";
            SealImageUserInfo userInfo = new SealImageUserInfo(sealPerson, sealLocation, sealReason, department, remark1, remark2, remark3);
            String sealImageStrategyXml = createSealImageStrategyXml(sealImageCode, bizSerialNo, businessCode, businessCodeStyle, userInfo);
            sealImageCode = "dddddd";
            bizSerialNo = GUID.generateId();
            businessCode = "00000001";
            String sealImageStrategyXml2 = createSealImageStrategyXml(sealImageCode, bizSerialNo, businessCode, businessCodeStyle, userInfo);
            //String sealImageStrategyListXml = "<List>" + sealImageStrategyXml + sealImageStrategyXml2 + "</List>";
            String sealImageStrategyListXml = "<List>" + sealImageStrategyXml + "</List>";
            // 取得生成的图片文件数据
            String result = clientBean.autoUseSealImage(sealImageStrategyListXml, operatorCode, channelCode);

            String code = StringUtil.getNodeText(result, "Code");
            if (!code.equals("200")) {
                System.out.println("autoUseSealImage");
                System.out.println(result);
            } else {System.out.println(result);
            System.out.println(result);
            String[] beanList = result.split("<Bean>");
            for(int i = 1,loop = beanList.length; i< loop; i++){
                String bean = beanList[i].split("</Bean>")[0];
                System.out.println("bean[" + i + "] >>>> " + bean);
                
                String sealImage = StringUtil.getXmlField(bean, "SealImage");
                IoUtil.write("./TestData/output/autoUseSealImage" + i + ".png", Base64.decode(sealImage));
                String businessCode2 = StringUtil.getXmlField(bean, "BusinessCode");
                System.out.println("businessCode" + i + " : " + businessCode2);
            }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static String createSealImageStrategyXml(String sealImageCode, String bizSerialNo, String businessCode, String businessCodeStyle, SealImageUserInfo userInfo) {
        
        StringBuilder sealImageStrategyXml = new StringBuilder();
        sealImageStrategyXml.append("<Request>");
        sealImageStrategyXml.append("<SealImageCode>").append(sealImageCode).append("</SealImageCode>");
        sealImageStrategyXml.append("<BizSerialNo>").append(bizSerialNo).append("</BizSerialNo>");
        if(StringUtil.isNotEmpty(businessCode)) {
            sealImageStrategyXml.append("<BusinessCode>").append(businessCode).append("</BusinessCode>");
            sealImageStrategyXml.append("<BusinessCodeStyle>").append(businessCodeStyle).append("</BusinessCodeStyle>");
        }
        sealImageStrategyXml.append("<SealPerson>").append(userInfo.getSealPerson()).append("</SealPerson>");
        sealImageStrategyXml.append("<SealReason>").append(userInfo.getSealReason()).append("</SealReason>");
        sealImageStrategyXml.append("<SealLocation>").append(userInfo.getSealLocation()).append("</SealLocation>");
        sealImageStrategyXml.append("<Department>").append(userInfo.getDepartment()).append("</Department>");
        sealImageStrategyXml.append("<Remark1>").append(userInfo.getRemark1()).append("</Remark1>");
        sealImageStrategyXml.append("<Remark2>").append(userInfo.getRemark2()).append("</Remark2>");
        sealImageStrategyXml.append("<Remark3>").append(userInfo.getRemark3()).append("</Remark3>");
        sealImageStrategyXml.append("</Request>");

        return sealImageStrategyXml.toString();
    }

}
